package com.example.server.controller;

import com.example.server.entity.Comment;
import com.example.server.entity.Product;
import com.example.server.entity.User;
import com.example.server.service.CommentService;
import com.example.server.util.Util;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.text.StyledEditorKit;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("comment")
public class CommentController extends BaseController{
    private final CommentService commentService;
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @PostMapping("create")
    @ResponseBody
    private boolean create(@RequestBody Comment comment){
        comment.setIp(request.getRemoteAddr());
//        String path = "D:/softwear/shixun/project-team06/photo/comment";
//        comment.setPicture(Util.savePicture(file,path));
        commentService.create(comment);
        System.out.println(comment.getPid());
        return true;
    }

    @GetMapping("query_comment_user_product")
    @ResponseBody
    private List<Comment> queryCommentUserProduct(@RequestParam BigInteger pid){
        List<Comment> comments = commentService.queryList("query_comment_user_product",pid);
        for(Comment comment:comments){
//            if (comment.getPicture() != null){
//                comment.setPicture(comment.getPicture().replaceAll("[\\[\\]\"]",""));
//            }
            comment.getUser().setUsername(Util.encryptNickname(comment.getUser().getUsername()));
        }
        return comments;
    }

    @DeleteMapping("removeById")
    @ResponseBody
    private boolean removeById(@RequestBody Map<String,BigInteger> map){
        BigInteger commid = map.get("commid");
        commentService.removeById(commid);
        return true;
    }

}
